package com.biao.seckill.controller;

import com.biao.common.utils.R;
import com.biao.seckill.service.SeckillService;
import com.biao.seckill.to.SeckillSkuRedisTo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api
@Slf4j
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前可以参与的秒杀商品信息
     *
     * @return
     */

    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    /**
     * 查询当前商品是否参与秒杀
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }


    /**
     * killId=3_1&key=f11ebbf60a144cf693571dc949097650&num=1
     *
     * @param killId 商品的场次和skuId
     * @param key    随机码
     * @param num    数量
     * @return
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
        //判断是否登录
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }

    @ApiOperation("商品定时任务上架功能")
    @ResponseBody
    @GetMapping("/upSeckillSkuLatest3Days")
    public String upSeckillSkuLatest3Days() {
        //1.重复上架无需处理
        log.info("上架秒杀商品信息");
        seckillService.upSeckillSkuLatest3Days();
        return "succ";
    }

}
